# Copyright (c) Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

function(gen_intrinsics_yaml)
    set(prefix ARG)
    set(noValues)
    set(singleValues OUTPUTFILE TARGET)
    set(multiValues DATAFILES DEPENDS)
    cmake_parse_arguments(${prefix}
            "${noValues}"
            "${singleValues}"
            "${multiValues}"
            ${ARGN})
    set(GENERATOR ${CMAKE_CURRENT_SOURCE_DIR}/templates/gen_intrinsics_data.rb ${CMAKE_CURRENT_SOURCE_DIR}/templates/runtime.rb)
    set(TEMPLATE ${CMAKE_CURRENT_SOURCE_DIR}/templates/intrinsics.yaml.erb)
    set(DEPENDS_LIST ${GENERATOR} ${TEMPLATE} ${ARG_DEPENDS})
    string(REPLACE ";" "," DATAFILES_STR "${ARG_DATAFILES}")
    foreach(d ${ARG_DATAFILES})
        list(APPEND DEPENDS_LIST ${d})
    endforeach()
    add_custom_command(OUTPUT ${ARG_OUTPUTFILE}
            COMMENT "Generate intrinsics.yaml"
            COMMAND ${GENERATOR} -d ${DATAFILES_STR} -t ${TEMPLATE} -o ${ARG_OUTPUTFILE}
            DEPENDS ${DEPENDS_LIST})
    add_custom_target(${ARG_TARGET} ALL DEPENDS ${ARG_OUTPUTFILE})
endfunction()

set(ECMASCRIPT_RUNTIME_YAML "")
if (PANDA_WITH_ECMASCRIPT)
    set(ECMASCRIPT_RUNTIME_YAML ${PANDA_ECMASCRIPT_PLUGIN_SOURCE}/runtime/ecma_runtime.yaml)
endif()

set(JAVA_RUNTIME_YAML "")
if (PANDA_WITH_JAVA)
    set(JAVA_RUNTIME_YAML ${PANDA_JAVA_PLUGIN_SOURCE}/runtime/libcore_runtime.yaml)
    if(NOT PANDA_TARGET_MOBILE_WITH_MANAGED_LIBS)
        list(APPEND JAVA_RUNTIME_YAML ${PANDA_JAVA_PLUGIN_SOURCE}/runtime/java_libbase_runtime.yaml)
    endif()
endif()

set(ACCORD_RUNTIME_YAML "")
if (PANDA_WITH_ACCORD)
    set(ACCORD_RUNTIME_YAML ${PANDA_ACCORD_PLUGIN_SOURCE}/runtime/accord_runtime.yaml)
endif()

set(INTRINSICS_YAML ${CMAKE_CURRENT_BINARY_DIR}/intrinsics.yaml)
set(INTRINSICS_TARGET arkruntime_gen_intrinsics_yaml)
set(DATAFILES_TARGET ${CMAKE_CURRENT_SOURCE_DIR}/runtime.yaml ${JAVA_RUNTIME_YAML} ${ECMASCRIPT_RUNTIME_YAML} ${ACCORD_RUNTIME_YAML} ${IRTOC_INTRINSICS_YAML})

gen_intrinsics_yaml(
    TARGET ${INTRINSICS_TARGET}
    DATAFILES ${DATAFILES_TARGET}
    OUTPUTFILE ${INTRINSICS_YAML}
)

set(RUNTIME_TEMPLATES
    intrinsics_gen.h.erb
    intrinsics.h.erb
    intrinsics_enum.h.erb
    unimplemented_intrinsics-inl.cpp.erb
    )

panda_gen(
    DATA ${INTRINSICS_YAML}
    TARGET_NAME intrinsics_gen_arkruntime
    TEMPLATES ${RUNTIME_TEMPLATES}
    REQUIRES ${CMAKE_CURRENT_SOURCE_DIR}/templates/intrinsics.rb
    SOURCE ${CMAKE_CURRENT_SOURCE_DIR}/templates
    DESTINATION ${GEN_INCLUDE_DIR}
    EXTRA_DEPENDENCIES ${INTRINSICS_TARGET}
)
